/*
 * Copyright (c) 2022，公司名称
 * All rights reserved.
 * 文件名称：high_tim.c
 * 摘要：高边PWM
 * 当前版本：V1.0.0,WCD,2023/1/10,初版
 * 历史版本：Vx.x.x，编写者/修改者，修改时间，修改内容（重大发布时，总结重点变更内容）
 */
#include "high_includes.h"
#define PWM_TIM_ID TIM0_ID
/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓应用层数据定义开始↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
#if 1
static float curr_duty = 0.0;
#endif
/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑应用层数据定义结束↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/
static TIM_PwmChannelConfig_t PwmChConfig[2] =
    {
        {.channelId = TIM_CHANNEL_0, //  TIM_CHANNEL_0
         .pwmModeConfig = TIM_PWM_HIGH_TRUE_PULSE,
         .polarity = TIM_POL_HIGH,
         .compareValue = (PWM_64M_200HZ * 50 / 100),
         .faultCtrlChannelEnable = DISABLE,
         .ccvUpdateEnable = ENABLE},
        {.channelId = TIM_CHANNEL_4, //  TIM_CHANNEL_0
         .pwmModeConfig = TIM_PWM_HIGH_TRUE_PULSE,
         .polarity = TIM_POL_HIGH,
         .compareValue = (PWM_64M_200HZ * 50 / 100),
         .faultCtrlChannelEnable = DISABLE,
         .ccvUpdateEnable = ENABLE}};

static TIM_PwmConfig_t PwmConfig[2] =
    {
        {
            1,
            0,              /*counter init value计数器初始值*/
            PWM_64M_200HZ,  /*counter max value计数器最大值*/
            &PwmChConfig[0] /*channel config pointer通道配置指针*/
        },
        {
            1,
            0,              /*counter init value计数器初始值*/
            PWM_64M_200HZ,  /*counter max value计数器最大值*/
            &PwmChConfig[1] /*channel config pointer通道配置指针*/
        }};
void high_tim_clock_init(void)
{
  /*TIM clock source select*/
  CLK_ModuleSrc(CLK_TIM0, CLK_SRC_FIRC64M);
  CLK_SetClkDivider(CLK_TIM0, CLK_DIV_1);
  /*TIM module enable*/
  SYSCTRL_ResetModule(SYSCTRL_TIM0);
  SYSCTRL_EnableModule(SYSCTRL_TIM0);
}
void high_tim_PWMOutputInit(void)
{
  /* set up-counting mode 设置计数模式*/
  TIM_CountingModeConfig(PWM_TIM_ID, TIM_COUNTING_UP);

  TIM_OutputEdgeAlignedPwmConfig(PWM_TIM_ID, &PwmConfig[0]);
  TIM_OutputEdgeAlignedPwmConfig(PWM_TIM_ID, &PwmConfig[1]);

  /* pair0 output enable pair0输出启用*/
  TIM_ChannelOutputEnable(PWM_TIM_ID, TIM_CHANNEL_0);
  TIM_ChannelOutputEnable(PWM_TIM_ID, TIM_CHANNEL_4);
  /*start TIM PWM*/
  TIM_StartCounter(PWM_TIM_ID, TIM_CLK_SOURCE_SYSTEM, TIM_CLK_DIVIDE_1);
  /*设置PWM输出周期 PWMhz计算：64M/8(CLK_CORE:8分频)/40000 = PWM为200Hz频率，20000PWM为400Hz频率*/
  TIM_SetCounterModVal(TIM0_ID, PWM_64M_200HZ);
	high_setpwm_Dutycycle(TIM0_ID, TIM_CHANNEL_0, 2048);
  high_setpwm_Dutycycle(TIM0_ID, TIM_CHANNEL_4, 2048);
}
void high_setpwm_Dutycycle(TIM_ID_t timId, TIM_ChannelId_t channelId, uint16_t ccVal)
{
  float a = (float)ccVal / 4095; // 获取百分比
  ccVal = PWM_64M_200HZ * a;
  /*设置PWM占空比*/
  TIM_SetCCVal(timId, channelId, ccVal);
}


/*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓应用层函数定义开始↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/
#if 1
float pwm_get_duty(void)
{
    return curr_duty;
}
/*
 * 函数介绍: IGBT1占空比设置
 * 参数:duty 占空比
 * 返回值: {*}
 * 备注:
 */
void PWM_IGBT1_duty(uint16_t duty)
{
    uint16_t ccVal = 0;
   curr_duty = duty / 4096.0;
   ccVal = PWM_64M_200HZ * curr_duty;
   TIM_SetCCVal(TIM0_ID, TIM_CHANNEL_0, ccVal);
}
/*
 * 函数介绍: IGBT2占空比设置
 * 参数:duty 占空比
 * 返回值: {*}
 * 备注:
 */
void PWM_IGBT2_duty(uint16_t duty)
{
        uint16_t ccVal = 0;
   curr_duty = duty / 4096.0;
   ccVal = PWM_64M_200HZ * curr_duty;
   TIM_SetCCVal(TIM0_ID, TIM_CHANNEL_4, ccVal);
}
/*
 * 函数介绍: 同时设置IGBT1、2占空比
 * 参数:duty 占空比
 * 返回值: {*}
 * 备注:
 */
void PWM_IGBT_duty(uint16_t duty)
{
   uint16_t ccVal = 0;
   curr_duty = duty / 4096.0;
   ccVal = PWM_64M_200HZ * curr_duty;
   TIM_SetCCVal(TIM0_ID, TIM_CHANNEL_0, ccVal);
   TIM_SetCCVal(TIM0_ID, TIM_CHANNEL_4, ccVal);
}
/*
 * 函数介绍: PWM关闭
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void pwm_off(void)
{
#if 1
    TIM_StopCounter(TIM0_ID);
    PWM_IGBT_duty(0);
#endif
}
/*
 * 函数介绍: PWM开启
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void pwm_on(void)
{
    /*start TIM PWM*/
  TIM_StartCounter(PWM_TIM_ID, TIM_CLK_SOURCE_SYSTEM, TIM_CLK_DIVIDE_1);
}
#endif
/*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑应用层函数定义结束↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/